package com.bnz.servlet;

import com.alibaba.fastjson.JSON;
import com.bnz.pojo.PageResult;
import com.bnz.pojo.R;
import com.bnz.pojo.Student;
import com.bnz.service.StudentService;
import com.bnz.service.impl.StudentServiceImpl;
import com.bnz.vo.StudentVo;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Struct;
import java.util.List;

/**
 * 类名：
 * 作者：WF
 * 功能：
 */
@WebServlet("/student")
public class StudentServlet extends HttpServlet {
	private StudentService studentService;
	@Override
	public void init() throws ServletException {
		studentService = new StudentServiceImpl();
	}
	//1. 进行请求的转发
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//1.1 处理中文乱码
		req.setCharacterEncoding("UTF-8");
		resp.setContentType("text/html;charset=utf-8");
		//1.2 得到用于转发的请求参数
		String cmd = req.getParameter("cmd");
		//1.3 根据请求参数转发到不同的方法
		if(StringUtils.isNotBlank(cmd)){
			if("list".equals(cmd)){         //列表学生
				list(req,resp);
			}
			if("search".equals(cmd)){       //条件查询带分页
				search(req,resp);
			}
			if("add".equals(cmd)){          // 添加学生
				add(req,resp);
			}
			if("update".equals(cmd)){       // 修改学生
				update(req,resp);
			}
			if("delete".equals(cmd)){       // 删除学生
				delete(req,resp);
			}
		}
	}


	//2. 列表所有学生
	private void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//2.1 查询所有学生
		List<Student> studentList = studentService.findAll();
		//2.2 转换为json串并输出
		resp.getWriter().print(JSON.toJSONString(studentList));
		//2.3 刷新
		resp.getWriter().flush();

	}

	//3. 条件查询带分页
	private void search(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//3.1 得到分页参数
		String pageStr = req.getParameter("page");
		String pagesizeStr = req.getParameter("pagesize");
		int page = StringUtils.isNotBlank(pageStr) ? new Integer(pageStr) : 1;
		int pagesize = StringUtils.isNotBlank(pagesizeStr) ? new Integer(pagesizeStr) : 5;
		//3.2 得到条件查询对象
		String studSearch = req.getParameter("studSearch");
		studSearch = new String(studSearch.getBytes("ISO8859-1"),"UTF-8");
		StudentVo vo = null;
		if(StringUtils.isNotBlank(studSearch)){
			vo = JSON.parseObject(studSearch,StudentVo.class);
		}
		//3.3 开始进行查询
		PageResult<Student> pr = studentService.search(page,pagesize,vo);
		System.out.println("pr = " + pr);
		//3.4 转换为字符串
		resp.getWriter().println(JSON.toJSONString(pr));
		resp.getWriter().flush();

	}

	//4. 添加学生
	private void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String info = "";
		try {
			//4.1 得到前端传入的数据
			String stud = req.getParameter("stud");
			//4.1.1 处理乱码
			stud = new String(stud.getBytes("ISO8859-1"), "UTF-8");
			//4.1.2 转换为学生对象
			Student student = JSON.parseObject(stud, Student.class);
			//4.2 添加到数据库中
			studentService.add(student);
			R ok = R.ok("添加学生成功!");
			info = JSON.toJSONString(ok);
		}catch (Exception e){
			e.printStackTrace();
			R error = R.error();
			info = JSON.toJSONString(error);
		}
		resp.getWriter().println(info);
		resp.getWriter().flush();
	}

	//5. 修改学生
	private void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String info = "";
		try {
			//5.1 得到前端传入的数据
			String stud = req.getParameter("stud");
			//5.1.1 处理乱码
			stud = new String(stud.getBytes("ISO8859-1"), "UTF-8");
			//5.1.2 转换为学生对象
			Student student = JSON.parseObject(stud, Student.class);
			//5.2 修改到数据库中
			studentService.update(student);
			R ok = R.ok("修改学生成功!");
			info = JSON.toJSONString(ok);
		}catch (Exception e){
			e.printStackTrace();
			R error = R.error();
			info = JSON.toJSONString(error);
		}
		resp.getWriter().println(info);
		resp.getWriter().flush();
	}

	//6. 删除学生
	private void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String info = "";
		try {
			//6.1 得到前端传入的数据
			String[] sids = req.getParameter("sids").split(",");
			//6.2 删除学生
			studentService.delete(sids);
			R ok = R.ok("删除学生成功!");
			info = JSON.toJSONString(ok);
		}catch (Exception e){
			e.printStackTrace();
			R error = R.error();
			info = JSON.toJSONString(error);
		}
		resp.getWriter().println(info);
		resp.getWriter().flush();

	}

}
